package com.woniuxy.main.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.session.Session;
import org.apache.shiro.web.session.HttpServletSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.woniuxy.main.pojo.Coach;
import com.woniuxy.main.pojo.Gym;
import com.woniuxy.main.pojo.Lesson;
import com.woniuxy.main.pojo.User;
import com.woniuxy.main.pojo.Vi_user_coach;
import com.woniuxy.main.service.CoachService;
import com.woniuxy.main.service.GymService;
import com.woniuxy.main.service.LessonService;
import com.woniuxy.main.service.UserService;
import com.woniuxy.main.service.Vi_user_coachService;
import com.woniuxy.main.util.SessionUtil;

import lombok.Data;

@Controller
@RequestMapping("/vi_user_coach")
@Data
public class Vi_user_coachHandler {
	@Resource
	private Vi_user_coachService vi_user_coachService;
	@Resource
	private CoachService coachService;
	@Resource
	private UserService userService;
	@Resource
	private LessonService lessonService;
	@Resource
	private GymService gymService;
	
	@RequestMapping("/all")
	@ResponseBody
	public List<Vi_user_coach> all() {
		List<Vi_user_coach> result = vi_user_coachService.all();
		return result;
	}
	@RequestMapping("/searchCoach")
	@ResponseBody
	public List<Vi_user_coach> searchCoach(String price_min,String price_max,String week,String style,int authentication) {
//		System.out.println(price_min);
//		System.out.println(price_max);
//		System.out.println(week);
//		System.out.println(style);
//		System.out.println(authentication);
		// 将所有数据存入map
		Map<String,Object> map = new HashMap<>();
		// 课程费数据合法性检查
		try {
			// 防止恶意输入的非数字数据
			if(!price_min.equals("")) {
				int priceMin = (int) Math.ceil(Double.parseDouble(price_min));
				map.put("priceMin", priceMin);
			} else {
				map.put("priceMin", price_min);
			}
			
			if(!price_max.equals("")) {
				int priceMax = (int) Math.floor(Double.parseDouble(price_max));
				map.put("priceMax", priceMax);
			} else {
				map.put("priceMax", price_max);
			}
			
		} catch (Exception e) {
			return null;
		}
		// 将week存入数组
		String[] weeks = week.split(",");
		if(weeks.length != 0) {
			for (int i = 0; i < weeks.length; i++) {
				weeks[i] = "%" + weeks[i] + "%";
			}
		}
		if(!style.equals(""))
		style = "%" + style + "%";
		map.put("weeks", weeks);
		map.put("style", style);
		map.put("authentication", authentication);
		
		List<Vi_user_coach> result = vi_user_coachService.searchCoach(map);
//		if(result != null)
//		System.out.println(result);
		return result;
	}

	@RequestMapping("/selectByUid")
	@ResponseBody
	public Vi_user_coach selectByUid(){
		//System.out.println("查询教练信息");
		//获取uid
		Session session = SessionUtil.getSession();
		int uid = (int) session.getAttribute("uid");
		//System.out.println(uid);
		//查询当前教练信息
		
		Vi_user_coach coach= vi_user_coachService.selectByUid(uid);
		return coach;	
	}
	
	// 查询当前选择的教练的详细信息
	@RequestMapping("/searchSelectCoachByUid")
	@ResponseBody
	public Map<String,Object> searchSelectCoachByUid(int uid){
		Map<String,Object> map = new HashMap<>();
		Vi_user_coach coach= vi_user_coachService.selectByUid(uid);
		map.put("coach",coach);
		// 根据gid查出场馆
		Gym gym = gymService.findGymByGid(coach.getGid());
		map.put("gym", gym);
		return map;	
	}
	
	//教练更新个人信息
	@ResponseBody
	@RequestMapping("/updateCoach")
	public String updateCoach(HttpServletRequest request,Coach coach,User user,Lesson lesson){
		String result="修改失败";
			Session session = SessionUtil.getSession();
			int uid  = (int) session.getAttribute("uid");
		//判断是否传入user的数据
		boolean uflag=false;
		String s=null;
		String mail=null;
		if(user.getNickName()!=null && user.getNickName().length()!=0){
			uflag=true;
		}
		if(user.getAddress()!=null && user.getAddress().length()!=0){
			uflag=true;
		}
		if(user.getUphone()!=null && user.getUphone().length()!=0){
			//判断是否存在相同的手机号
			s=userService.isPhoneExist(user.getUphone());
			uflag=true;
		}
		if(user.getUmail()!=null && user.getUmail().length()!=0){
			//判断是否存在相同的邮箱
			mail=userService.isMailExist(user.getUmail());
			uflag=true;
		}			
		user.setUid(uid);
		
		if(s==null && mail==null){
		if(uflag){
		userService.updateUser(user);
			}
		}else{
			return "失败,手机号码或邮箱已存在";
		}
		coach.setUid(uid);		
		int row=coachService.updateCoach(coach);
		if(row!=0){
			result="信息修改成功";
		}
		
		boolean lflag=false;
		 if(lesson.getContent()!=null ||lesson.getContent().length()!=0){
			 lflag=true;
		 }
		 if(lesson.getTime()!=null||lesson.getTime().length()!=0){
			 lflag=true;
		 }
		//查出当前教练的cid
		Coach c =coachService.selectByUid(coach.getUid());
		lesson.setCid(c.getCid());
		if(lflag){
		lessonService.updateLesson(lesson);
		}
		HttpSession session1 =request.getSession();	
	session1.setAttribute("address", user.getAddress());
	session.setAttribute("address", user.getAddress());
	
	return result;
	}
	
	@RequestMapping("/showCoachList")
	@ResponseBody
	public Map<String,Object> showCoachList(Integer page){
		//System.out.println("开始运行");
		int index = (page - 1) * 8;
		Map<String,Object> result=new HashMap<String,Object>();
		List<Vi_user_coach> info=vi_user_coachService.showCoachList(index);
		result.put("form", info);
		//查询总页码
		int totalCount=vi_user_coachService.totalPage();
		int totalPage=totalCount % 8 == 0 ? totalCount / 8 : totalCount / 8 + 1;
		result.put("totalPage", totalPage);
		return result;
	}
	// 跳转到附近教练页面
	@RequestMapping("/viewCoach")
	public ModelAndView viewCoach() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/viewCoach.html");
		return mav;
	}
	
	// 跳转到附近场馆页面
	@RequestMapping("/viewGym")
	public ModelAndView viewGym() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/viewGym.html");
		return mav;
	}
	
}
